package org.tissou.server.Base;

import org.tissou.server.Utils.InterfaceMySQL;
import org.tissou.client.Services.MonException;
import org.tissou.server.Utils.Variables_Globales;

import java.sql.SQLException;

import org.tissou.client.Services.MonnaieInfos;

/**
 * Created by IntelliJ IDEA.
 * User: Claire
 * Date: 12-Jan-2009
 * Time: 19:12:56
 * To change this template use File | Settings | File Templates.
 */
public class Monnaie {

  private MonnaieInfos infos;

  public Monnaie(MonnaieInfos infos){
    this.infos = infos;
  }

  public static Monnaie ChargerMonnaie (String nom){
    MonnaieInfos informations = new MonnaieInfos(nom);
    return new Monnaie(informations);
  }

  public static Monnaie ChargerMonnaie (MonnaieInfos informations){
    return new Monnaie(informations);
  }

  public static void CreerMonnaie (String nom){
    try {
      InterfaceMySQL.getInstance().EnvoyerQuery(
          String.format("INSERT INTO monnaies(NomMonnaie) VALUES('%s')", nom));
    } catch (SQLException e) {
      if(e.getErrorCode() == 1062){
        throw new MonException("La monnaie existe deja.",e);
      }
      else throw new MonException("Impossible de creer la monnaie dans la base de donnees",e);
    }
    Variables_Globales.ListeMonnaies = Monnaie_Ensemble.ChargerEnsembleMonnaies();
  }

  public boolean equals(Object monnaie){
    return infos.equals(monnaie);
  }

  public String getNomMonnaie() {
    return infos.getNomMonnaie();
  }

  public MonnaieInfos getInfos() {
    return infos;
  }
}
